{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7bafc9b9-ab54-4872-b947-86f245471d27",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "222406ca927e48dcade09e259296de6d",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Loading checkpoint shards:   0%|          | 0/2 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "模型已加载，现在可以开始提问了！输入 'exit' 退出。\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入问题:  实初哥哥这么说，就枉顾我们一直以来的兄妹情谊了，嬛儿没有哥哥\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Setting `pad_token_id` to `eos_token_id`:None for open-end generation.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "模型回答: 实初哥哥这么说，就枉顾我们一直以来的兄妹情谊了，嬛儿没有哥哥的照顾，我实在不放心。\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入问题:  实初哥哥这么说，就枉顾我们一直以来的兄妹情谊了，\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Setting `pad_token_id` to `eos_token_id`:None for open-end generation.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "模型回答: 实初哥哥这么说，就枉顾我们一直以来的兄妹情谊了，实初哥哥竟要这样对我们。\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入问题:  妹妹今日打扮得好生素净\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Setting `pad_token_id` to `eos_token_id`:None for open-end generation.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "模型回答: 妹妹今日打扮得好生素净，像极了皇上心中心爱的甄嬛。\n"
     ]
    }
   ],
   "source": [
    "from peft import PeftModel\n",
    "from transformers import AutoModelForCausalLM, AutoTokenizer\n",
    "\n",
    "# 指定基础模型的路径\n",
    "model_name = \"/root/autodl-tmp/deepseek-r1-distill-llama-8b\"  # 替换为你的模型路径\n",
    "tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=False, trust_remote_code=True)\n",
    "model = AutoModelForCausalLM.from_pretrained(model_name, device_map={\"\": 0}, trust_remote_code=True)\n",
    "\n",
    "# 指定LoRA适配器的路径\n",
    "lora_path = \"./Llama3_8b_LoRA_2025\"  # 替换为你的LoRA适配器保存路径\n",
    "model = PeftModel.from_pretrained(model, lora_path)\n",
    "\n",
    "# 定义生成回答的函数\n",
    "def generate_response(model, tokenizer, prompt, max_length=128):\n",
    "    inputs = tokenizer(prompt, return_tensors=\"pt\").to(model.device)\n",
    "    outputs = model.generate(**inputs, max_length=max_length, eos_token_id=tokenizer.eos_token_id)\n",
    "    response = tokenizer.decode(outputs[0], skip_special_tokens=True)\n",
    "    return response\n",
    "\n",
    "# 交互模式\n",
    "print(\"模型已加载，现在可以开始提问了！输入 'exit' 退出。\")\n",
    "while True:\n",
    "    user_input = input(\"请输入问题: \")\n",
    "    if user_input.lower() == 'exit':\n",
    "        print(\"退出程序。\")\n",
    "        break\n",
    "    response = generate_response(model, tokenizer, user_input)\n",
    "    print(\"模型回答:\", response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b0fdb6ea-7fb3-4a4a-b556-e29de251dd80",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
